'''
Created on Nov 14, 2010

@author: linh-a
'''

from PyQt4 import QtCore, QtGui, Qt
from core.Category import Category
from types import NoneType
from util.CategoryManager import CategoryManager

class MyTreeWidget( QtGui.QTreeWidget ):
  '''
  This class extend QTreeWidget of Qt, drag and drop is overrided.
  '''
  
  def __init__( self, conn = None, parrent = None ):
    super( MyTreeWidget, self ).__init__( parrent )
    self.conn = conn
    #enable drag drop
    self.setDragDropMode( self.InternalMove )
    self.setDragEnabled( True )
    self.setDropIndicatorShown( True )
    
  def dropEvent( self, event ):
    cont = True
    #source item:
    srcItem = self.currentItem()
    srcData = srcItem.data( 0, Qt.Qt.UserRole )
    srcCat = srcData.toPyObject()
    if type( srcCat ) is not Category:
      #root item
      cont = False
      return
    #destination item:
    destItem = self.itemAt( event.pos() )
    if type( destItem ) is NoneType:
      #nothing
      cont = False
      return
    destData = destItem.data( 0, Qt.Qt.UserRole )
    destCat = destData.toPyObject()
    if type( destCat ) is not Category:
      askResult = QtGui.QMessageBox.question( None, "Confirm", "Are you sure to move %s into root category?" % ( srcCat.get_name() ), buttons = QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, defaultButton = QtGui.QMessageBox.Yes )
    else:
      askResult = QtGui.QMessageBox.question( None, "Confirm", "Are you sure to move %s into %s?" % ( srcCat.get_name(), destCat.get_name() ), buttons = QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, defaultButton = QtGui.QMessageBox.Yes )
    if askResult == QtGui.QMessageBox.Yes:
      #move in database
      catMan = CategoryManager( self.conn )
      if type( destCat ) is not Category:
        #root item --> move to level 0
        catMan.moveToLevel0( srcCat )
      else:
        #normal item --> change parent
        catMan.moveToChildOf( srcCat, destCat )
      #update model
      self.updateData( self.conn )
  def updateData( self, conn = None ):
    if conn is None and self.conn is None:
      QtGui.QMessageBox.critical( None, "Error", "Database connection is not available", QtGui.QMessageBox.Ok, QtGui.QMessageBox.Ok )
    else:
      self.clear()
      rootItem = QtGui.QTreeWidgetItem( self, ['Note categories'] )
      #get data
      if conn is None:
        catMan = CategoryManager( self.conn )
      else:
        catMan = CategoryManager( conn )
      catl0s = catMan.getCategoryLevel0()
      #fill data
      if type( catl0s ) is not NoneType:
        for cat in catl0s:
          c = QtGui.QTreeWidgetItem( rootItem, [cat.get_name()] )
          c.setData( 0, Qt.Qt.UserRole, Qt.QVariant( cat ) )
          #c.setFlags( Qt.Qt.ItemIsEnabled | Qt.Qt.ItemIsEditable | Qt.Qt.ItemIsDragEnabled | Qt.Qt.ItemIsDropEnabled )
          cs = catMan.getChildCategory( cat )
          if type( cs ) is not NoneType:
            for ca in cs:
              cc = QtGui.QTreeWidgetItem( c, [ca.get_name()] )
              cc.setData( 0, Qt.Qt.UserRole, Qt.QVariant( ca ) )
              #cc.setFlags( Qt.Qt.ItemIsEnabled | Qt.Qt.ItemIsEditable | Qt.Qt.ItemIsDragEnabled | Qt.Qt.ItemIsDropEnabled )
      #expand root
      rootItem.setExpanded( True )
